package swu.cx.androidmvc.Model

import kotlinx.android.synthetic.main.activity_main.*
import swu.cx.androidmvc.MainActivity
import swu.cx.androidmvc.Model.DataBase.dataBase

class BookRepository(private val dao: BookDao) {
    companion object{
        @Volatile private var instance: BookRepository?=null
        fun getInstance(dao: BookDao)= instance?: synchronized(this){
            instance?: BookRepository(dao).also { instance = it }
        }
    }
    fun getBooks():List<Book>{
        return dao.getBooks()
    }
    fun addBook(book: Book,mainActivity: MainActivity){
        dao.addBook(book)
        val builder = StringBuilder()
        getBooks().forEach {book ->
            builder.append("${book.name}-${book.author} \n")
        }
        mainActivity.upDateUI(builder.toString())
    }
}